<?php
	require_once('../../auth/session.php');
//get values from POST
	$title=$_POST['title'];        
	$firstname=$_POST['firstname'];    
 	$lastname=$_POST['lastname'];     
	$gender=$_POST['gender'];    
  	$phone=$_POST['phone'];
	$email=$_POST['email'];
	$password=$_POST['password'];
	$type=$_POST['acctType'];
	$login_id = $email; 
	$status=1; //default: pending
	$created = date('Y-m-d H:i:s');
	
	//$status
	if($type==1){ // Volunteer request
		$status=0; // request set to pending
	} 
	
include 'db_Connect.php';

	$username_exists_query = "SELECT user_id FROM User WHERE login_id='".$login_id."'";
			$username_exists_results = mysql_query($username_exists_query);
			$username_exists=mysql_num_rows($username_exists_results);  
	
	if($username_exists==0){
    
		$account = "INSERT INTO User 
					(title, first_name, last_name, gender, email_address, phone_number, login_id, password, user_type, active_status, created)
				VALUES
		         ('$title', '$firstname', '$lastname', '$gender', '$email', '$phone', '$login_id', '".SHA1($password)."', '$type', '$status', '$created')";
		
		$result = mysql_query($account);
		if($result){
			
				$last_user_id_query = "SELECT user_id FROM User WHERE login_id='".$login_id."' AND password='".SHA1($password)."'";
		
				$last_user_id_results = mysql_query($last_user_id_query);
				$last_user_id  = mysql_fetch_array($last_user_id_results);
				$user_id = $last_user_id['user_id'];
						
					if($type==1){//If volunteer
							$_SESSION['ACCT_CREATE_STATUS'] = 'Volunteer Account Request Successfully Sent';
					}
					
					if($type==2){//If Adoptive User
					
					// Add preference entry to datatable
						$preferences_query = "INSERT INTO AdoptionPreferences (user_id) VALUES ('$user_id')";
				
						$preferences = mysql_query($preferences_query);
					
						if($preferences){	
								$_SESSION['ACCT_CREATE_STATUS'] = "Adoptive User Account Successfully Created";
						} else {
								$_SESSION['ACCT_CREATE_STATUS'] = "Adoptive User Preference Creation Failed";
						}	
						
					// Add gift status entry to datatable
						$giftStatus_query = "INSERT INTO GiftDelivery (user_id) VALUES ('$user_id')";
				
						$giftStatus = mysql_query($giftStatus_query);
					
						if($giftStatus){	
								$_SESSION['ACCT_CREATE_STATUS'] = "Adoptive User Account Successfully Created";
						} else {
								$_SESSION['ACCT_CREATE_STATUS'] = "Adoptive User Preference Creation Failed";
						}
				}
			
		} else{
				$_SESSION['ACCT_CREATE_STATUS'] = "Adoptive User Account Creation Failed";
		}
	} else {
				$_SESSION['ACCT_CREATE_STATUS'] = "Username already exists!";
	}
		session_write_close();
		header("location: ../../index.php");
		exit();

include 'db_Close.php';
?>
